package com.kingbi.system.user.service;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.apache.commons.beanutils.BasicDynaBean;

import com.kingbi.system.user.dao.UserDao;
import com.kingbi.system.user.jsfBean.UserDataBean;
import com.kingbi.system.userRole.dao.UserRoleDao;

/**
 * 用户实现类
 * 
 */
public class UserServiceImpl implements UserService {

	private UserDao userDao;
	private UserRoleDao userRoleDao;
	
	/**
	 * 查询数据总记录数
	 */
	public long searchUserCount(UserDataBean userDataBean) throws Exception {
		long num = 0l;
		try {
			StringBuffer sbsql = new StringBuffer();
			sbsql.append(" select * from  p_user pu where pu.valid=1 ");
			if(userDataBean!=null && !"".equals(userDataBean.getQueryName()) && null!=userDataBean.getQueryName()){
				sbsql.append(" and pu.user_name like '%").append(userDataBean.getQueryName()).append("%'");
			}
			
			List<Object> objList = userDao.findBySQL(sbsql.toString(),null);
			String objSize = String.valueOf(objList.size());
			num = Long.valueOf(objSize);
		} catch (Exception e) {
			e.printStackTrace();
			return num;
		}
		return num;
	}
	
	
	/**
	 * 查询所有用户数据
	 */
	public List<UserDataBean> searchUserData(UserDataBean userDataBean,int start,int max){
		StringBuffer sbsql = new StringBuffer();
		sbsql.append(" select pu.user_id,pu.user_name,pu.phone,pu.mobile,pu.email,pu.remark,");
		sbsql.append(" group_concat(pr.role_code) role_code,group_concat(pr.role_name) role_name");
		sbsql.append(" from p_user pu ");
		sbsql.append(" left join p_user_role pur on pur.user_id=pu.user_id");
		sbsql.append(" left join p_role pr on pr.role_code=pur.role_code");
		sbsql.append(" where pu.valid=1 ");
		if(userDataBean!=null && !"".equals(userDataBean.getQueryName()) && null!=userDataBean.getQueryName()){
			sbsql.append(" and pu.user_name like '%").append(userDataBean.getQueryName()).append("%'");
		}
		sbsql.append(" group by pu.user_id,pu.user_name,pu.phone,pu.mobile,pu.email,pu.remark");
		sbsql.append(" order by pu.user_id");
		
		List<UserDataBean> resultList = new ArrayList<UserDataBean>();
		try {
			List<Object> objList = userDao.findBySQL(sbsql.toString(), null, start, max);
			if(!objList.isEmpty()){
				Iterator<Object> iter = objList.iterator();
				BasicDynaBean bean = null;
				while(iter.hasNext()){
					bean = (BasicDynaBean) iter.next();
					UserDataBean tempBean = new UserDataBean();
					
					tempBean.setUserId(bean.get("user_id")!=null ? bean.get("user_id").toString():"");
					tempBean.setUserName(bean.get("user_name")!=null ? bean.get("user_name").toString():"");
					tempBean.setPhone(bean.get("phone")!=null ? bean.get("phone").toString():"");
					tempBean.setMobile(bean.get("mobile")!=null ? bean.get("mobile").toString():"");
					tempBean.setEmail(bean.get("email")!=null ? bean.get("email").toString():"");
					tempBean.setRemark(bean.get("remark")!=null ? bean.get("remark").toString():"");
					tempBean.setRoleCode(bean.get("role_code")!=null ? bean.get("role_code").toString():"");
					tempBean.setRoleName(bean.get("role_name")!=null ? bean.get("role_name").toString():"");
					resultList.add(tempBean);
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
			return resultList;
		}
		return resultList;
	}
	
	/**
	 * 查询所有用户角色信息
	 * @throws SQLException 
	 */
	public List<UserDataBean> getRoleInfo() throws SQLException{
		StringBuffer sbsql = new StringBuffer();
		sbsql.append(" select pr.role_code,pr.role_name from p_role pr");
		List<Object> objList = userDao.findBySQL(sbsql.toString(), null);
		List<UserDataBean> resultList = new ArrayList<UserDataBean>();
		
		if(!objList.isEmpty()){
			Iterator<Object> iter = objList.iterator();
			BasicDynaBean bean = null;
			while(iter.hasNext()){
				bean = (BasicDynaBean) iter.next();
				UserDataBean userDataBean = new UserDataBean();
				String roleCode = bean.get("role_code")!=null ? bean.get("role_code").toString():"";
				String roleName = bean.get("role_name")!=null ? bean.get("role_name").toString():"";
				
				userDataBean.setRoleCode(roleCode);
				userDataBean.setRoleName(roleName);
				resultList.add(userDataBean);
			}
		}
		return resultList;
	}
	
	

	public UserDao getUserDao() {
		return userDao;
	}

	public void setUserDao(UserDao userDao) {
		this.userDao = userDao;
	}

	public UserRoleDao getUserRoleDao() {
		return userRoleDao;
	}

	public void setUserRoleDao(UserRoleDao userRoleDao) {
		this.userRoleDao = userRoleDao;
	}

}
